package cn.itsource.system.service.impl;

import cn.itsource.system.domain.Role;
import cn.itsource.system.mapper.RoleMapper;
import cn.itsource.system.service.IRoleService;
import cn.itsource.basic.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huang
 * @since 2022-07-14
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void add(Role role) {
        //先保存角色
        roleMapper.insert(role);
        //保存角色菜单中间表
        //保存角色权限中间表
        initRelation(role);
    }

    private void initRelation(Role role) {
        roleMapper.removeRoleAndMenuByRoleId(role.getId());
        roleMapper.removeRoleAndPermissionByRoleId(role.getId());
        roleMapper.insertRoleAndMenu(role.getId(),role.getMenus());
        roleMapper.insertRoleAndPermission(role.getId(),role.getPermissions());
    }
}
